/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import data.OrganisasiData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Muhammad Dony
 */
public class OrganisasiDB extends ConnectDB {

    private String sql;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    
    public List getorganisasi() throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `organisasi`";
        return getorganisasidata(sql);
    }
    
    public List getorganisasiById(String ID) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `organisasi` WHERE `ID`='"+ID+"';";
        return getorganisasidata(sql);
    }
    
    public List getorganisasiByxx(String ID, int no) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `organisasi` WHERE `ID`='"+ID+"' AND `nomor`='"+no+"';";
        return getorganisasidata(sql);
    }
        
     private List getorganisasidata(String sql) throws SQLException, ClassNotFoundException{
        // mempersiapkan list dataPelaporan
        List listorganisasi = new ArrayList();
        //membuat koneksi , createConnection terdapat dalam DBClass
        con = createConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        // mempersiapkan object dataPelaporan
        OrganisasiData organisasiUser;
        
        while(rs.next()){
            organisasiUser = new OrganisasiData();
            organisasiUser.setNomor(rs.getInt(1));
            organisasiUser.setID(rs.getString(2));
            organisasiUser.setOrganisasi(rs.getString(3));
            organisasiUser.setJabatan(rs.getString(4));
            organisasiUser.setAwal(rs.getString(5));
            organisasiUser.setAkhir(rs.getString(6)); 
            listorganisasi.add(organisasiUser);
        }
        // menutup semua koneksi db
        rs.close();
        stmt.close();
        con.close();
        //return value list data pelaporan
        return listorganisasi;
        
    }
    
    private void actorganisasi(String sql) throws ClassNotFoundException, SQLException{
        // membuat koneksi
        con = createConnection();
        stmt = con.createStatement();
        // execute query
        stmt.execute(sql);
        // menutup koneksi db
        stmt.close();
        con.close();
    }
   public void insertOrganisasi(OrganisasiData listOrganisasi) throws ClassNotFoundException, SQLException{
        // query
        sql = "INSERT INTO `scholarship`.`organisasi` (`ID`, `organisasi`, `jabatan`, `awal`, `akhir`) VALUES ('"
                +listOrganisasi.getID()+"', '"
                +listOrganisasi.getOrganisasi()+"', '"
                +listOrganisasi.getJabatan()+"', '"
                +listOrganisasi.getAwal()+"', '"
                +listOrganisasi.getAkhir()+"');";
        actorganisasi(sql);
    }
   

   //// EDIT DAN DELETE MASIH BERMASALAH Y!!!!!
   
   
    public void updateOrganisasi(OrganisasiData listOrganisasi) throws ClassNotFoundException, SQLException{
        // query
        sql = "UPDATE `organisasi` SET `organisasi`='"+listOrganisasi.getOrganisasi()+
                "',`jabatan`='"+listOrganisasi.getJabatan()+
                "',`awal`='"+listOrganisasi.getAwal()+
                "',`akhir`='"+listOrganisasi.getAkhir()
                +"' WHERE `ID`='"+listOrganisasi.getID()+"'AND nomor="+listOrganisasi.getNomor()+";";
        // memanggil method untuk execute query
        actorganisasi(sql);
    }
    public void deleteOrganisasi(String ID, int organisasi) throws ClassNotFoundException, SQLException{
        sql = "DELETE FROM `organisasi` WHERE `ID`='"+ID+"' AND `nomor`='"+organisasi+"';";
        actorganisasi(sql);
    } 
    
    
    
}
